package com.iplus.ydkf.ehome.module.login;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.iplus.ydkf.ehome.App;
import com.iplus.ydkf.ehome.R;
import com.iplus.ydkf.ehome.common.utils.RegexValidateUtil;
import com.iplus.ydkf.ehome.module.MainActivity;
import com.iplus.ydkf.ehome.module.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by liuhaitian on 16/4/19.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.et_login_username)
    EditText et_login_username;
    @Bind(R.id.et_login_password)
    EditText et_login_password;

    @OnClick(R.id.btn_login)
    public void login() {
        if (isEmptyInput())
            return;
        AVUser.loginByMobilePhoneNumberInBackground(et_login_username.getText().toString(), et_login_password.getText().toString(), new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (isNoException(e)) {
                    App.flushCurrentUser();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                }
            }
        });
    }

    @OnClick(R.id.tv_login_register)
    public void register() {
        RegisterActivity.goActivity(this);
    }

    @OnClick(R.id.tv_login_forgetPwd)
    public void forgetPwd() {
        startActivity(new Intent(this, ForgetPwdActivity.class));
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initData(View view) {

    }

    @Override
    public String getFragmentName() {
        return null;
    }


    private boolean isEmptyInput() {
        if (TextUtils.isEmpty(et_login_username.getText().toString())) {
            snackbar("手机号不能为空");
            return true;
        }
        if (TextUtils.isEmpty(et_login_password.getText().toString())) {
            snackbar("密码不能为空");
            return true;
        }
        if (!RegexValidateUtil.checkCellphone(et_login_username.getText().toString())) {
            snackbar("请输入正确的手机号");
            return true;
        }
        return false;
    }
}
